/*
 * ioGame
 * Copyright (C) 2021 - present  渔民小镇 （262610965@qq.com、luoyizhu@gmail.com） . All Rights Reserved.
 * # iohao.com . 渔民小镇
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.iohao.example.sdk.logic.action;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iohao.example.sdk.logic.data.LoginVerifyMessage;
import com.iohao.example.sdk.logic.data.SdkGameCodeEnum;
import com.iohao.example.sdk.logic.data.UserMessage;
import com.iohao.example.sdk.logic.model.Cmd;
import com.iohao.game.action.skeleton.annotation.ActionController;
import com.iohao.game.action.skeleton.annotation.ActionMethod;
import com.iohao.game.action.skeleton.core.flow.FlowContext;
import com.iohao.game.bolt.broker.client.kit.ExternalCommunicationKit;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;

/**
 * My Action; 我的 Action
 *
 */
@ActionController(2)
@Slf4j
public final class MyAction {
    /**
     * this is hello action. 这是我提供的 hello action。
     *
     * @param name your name; 你的名字
     * @return 我的响应内容; My Response
     */
    // 静态共享 ObjectMapper 实例（线程安全）
    private static final ObjectMapper objectMapper = new ObjectMapper();


    @ActionMethod(SdkCmd.loginVerify)
    public UserMessage loginVerify(LoginVerifyMessage verifyMessage, FlowContext flowContext) {
        UserMessage userMessage = ofUserMessage(verifyMessage);

        ExternalCommunicationKit.forcedOffline(userMessage.userId);

        // 绑定 userId，表示登录
        var success = flowContext.bindingUserId(userMessage.userId);
        SdkGameCodeEnum.loginError.assertTrue(success);

        return userMessage;
    }

        private UserMessage ofUserMessage(LoginVerifyMessage loginVerifyMessage) {
        long userId = Long.parseLong(loginVerifyMessage.jwt);
        return ofUserMessage(userId);
    }

    private UserMessage ofUserMessage(long userId) {
        UserMessage userMessage = new UserMessage();
        userMessage.userId = userId;
//        userMessage.name = name.fullName();
        userMessage.name = String.valueOf(userId);
        return userMessage;
    }

    @ActionMethod(SdkCmd.stringValue)
    public String getCmd(String value){
        try {
            // 执行反序列化操作
            Cmd cmd = objectMapper.readValue(value, Cmd.class);
            log.info("info:"+cmd.getInfo()+",,  cmd:" + cmd.getCmd());
            return cmd.toString();
        } catch (JsonProcessingException e) {
            // 统一异常处理（可根据需求替换为日志记录）
            throw new IllegalArgumentException("JSON解析失败: " + value, e);
        }


    }


}
